//
// Created by XUAN on 2024/8/24.
//
/*
 * Created by XUAN on 2024/8/24.
 * NVS操作
 * 1、读取wifi列表
 * 2、写入wifi列表
 */

#include <nvs.h>
#include <esp_log.h>
#include "nvs_operation.h"
#include "cJSON.h"
#include <esp_heap_caps.h>
#include <stdbool.h>
#include <string.h>

static const char *TAG = "NVS_OPERATION";
bool nvs_read_wifi_list(char *str, size_t len){
    nvs_handle_t my_handle;
    esp_err_t err = nvs_open("WIFILIST", NVS_READWRITE, &my_handle);
    if (err != ESP_OK) {
        ESP_LOGE(TAG,"Error (%s) opening NVS handle\n", esp_err_to_name(err));
        return false;
    }
    err = nvs_get_str(my_handle, "wifi_list", str, &len);
    if (err != ESP_OK) {
        ESP_LOGE(TAG,"Error (%s) reading!\n", esp_err_to_name(err));
        nvs_close(my_handle);
        return false;
    }
    nvs_close(my_handle);
    ESP_LOGI(TAG,"Read wifi_list from NVS: %s\n", str);
    return true;
}
bool nvs_write_wifi_list(char *str){
    nvs_handle_t my_handle;
    esp_err_t err = nvs_open("WIFILIST", NVS_READWRITE, &my_handle);
    if (err != ESP_OK) {
        ESP_LOGE(TAG,"Error (%s) opening NVS handle\n", esp_err_to_name(err));
        return false;
    }
    err = nvs_set_str(my_handle, "wifi_list", str);
    err = nvs_commit(my_handle);
    if (err != ESP_OK) {
        ESP_LOGE(TAG,"Error (%s) writing!\n", esp_err_to_name(err));
        nvs_close(my_handle);
        return false;
    }
    nvs_close(my_handle);
    ESP_LOGI(TAG,"Write wifi_list to NVS: %s\n", str);
    return true;
}


bool reset_wifi_list(void) {
    // 分配空间读取 WiFi 列表
    char *nvs_wifi_list = (char *)heap_caps_malloc(1000, MALLOC_CAP_SPIRAM);
    if (nvs_wifi_list == NULL) {
        ESP_LOGE(TAG, "Memory for nvs_wifi_list is not enough");
        return false;
    }

    // 读取 NVS 中的 WiFi 列表
    if (!nvs_read_wifi_list(nvs_wifi_list, 1000)) {
        ESP_LOGE(TAG, "Failed to read WiFi list from NVS");
        free(nvs_wifi_list);
        return false;
    }

    // 解析读取到的 WiFi 列表
    cJSON *json = cJSON_Parse(nvs_wifi_list);
    if (json == NULL) {
        ESP_LOGE(TAG, "Failed to parse WiFi list JSON");
        free(nvs_wifi_list);
        return false;
    }

    // 获取 last 字段的值
    cJSON *last_item = cJSON_GetObjectItem(json, "last");
    if (last_item == NULL || !cJSON_IsString(last_item)) {
        ESP_LOGE(TAG, "Failed to get last item");
        cJSON_Delete(json);
        free(nvs_wifi_list);
        return false;
    }

    // 获取 last 的值，并删除对应的 WiFi 信息
    int last_index = atoi(last_item->valuestring);
    if (last_index > 0) {
        char index_str[12];
        snprintf(index_str, sizeof(index_str), "%d", last_index);
        cJSON_DeleteItemFromObject(json, index_str);  // 删除 last 对应的 WiFi 信息

        // 更新 last 字段为 "0"
        cJSON_ReplaceItemInObject(json, "last", cJSON_CreateString("0"));
    } else {
        ESP_LOGI(TAG, "No WiFi entry to delete, last is already 0");
    }

    // 将更新后的 JSON 转换为字符串
    char *updated_wifi_list = cJSON_PrintUnformatted(json);
    cJSON_Delete(json);
    free(nvs_wifi_list);  // 释放之前分配的空间

    // 将更新后的 WiFi 列表写回 NVS
    if (!nvs_write_wifi_list(updated_wifi_list)) {
        ESP_LOGE(TAG, "Failed to write updated WiFi list to NVS");
        free(updated_wifi_list);
        return false;
    }

    ESP_LOGI(TAG, "Updated WiFi list written to NVS: %s", updated_wifi_list);
    free(updated_wifi_list);
    return true;
}